package com.csproj.siiat.base.common.util.TLMessage;

import java.net.ServerSocket;
import java.text.DecimalFormat;

import com.csproj.basic.domain.ElectronicLockMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by tyshi on 2018/9/26.
 * 
 * this is the parent class for TL messages sent from guansuo, so called TL
 * 
 */

//TODO: add the validation function
public class TLMessage  {

    private final Logger logger = LoggerFactory.getLogger(TLMessage.class);
    public static final String AAHeader = "AA";
    public static final String C4MessageID = "C4";
    public static final String C5MessageID = "C5";
    public static final int C4MessageLength = 55;
    public static final int C5MessageLength = 33;

    private String header = "";
    private String len = "";
    private String id = "";
    private String dn = "";
    private TLMessageBody body = new TLMessageBody();
    private String vl = "";
    private String raw_string = "";

    public  TLMessage(byte[] b) {
    	raw_string = bytes2Hex(b);
        int index = 0;
        header = byte2Hex(b[index++]);
        len = String.valueOf(Integer.parseInt(byte2Hex(b[index++]),16));
        id = byte2Hex(b[index++]);
        byte[] tmp = new byte[4];
        System.arraycopy( b, index, tmp,0, tmp.length);
        index = index + tmp.length;
        dn = bytes2DN(tmp);

        if (id.equals(C4MessageID)) {
            tmp = new byte[46];
            System.arraycopy( b, index, tmp,0, tmp.length);
            index = index + tmp.length;
            body = new C4MessageBody(tmp);

        } else if (id.equals(C5MessageID)) {
            tmp = new byte[24];
            System.arraycopy( b, index, tmp,0, tmp.length);
            index = index + tmp.length;
            body = new C5MessageBody(tmp);

        }

        tmp = new byte[2];
        System.arraycopy( b, index, tmp,0, tmp.length);
        index = index + tmp.length;
        vl = bytes2Hex(tmp);
    }

	public static String byte2Hex (byte a) {
        Byte v = Byte.valueOf(a);
        return String.format("%02X", v.intValue() & 0xFF);
    }

    public static String byte2Int (byte a ) {
        Byte v = Byte.valueOf(a);
        return String.valueOf(v.intValue());
    }

    public static String byte2Int2 (byte a ) {
        Byte v = Byte.valueOf(a);
        return String.format("%02d",v.intValue());
    }
    public static String byte2String(byte a) {
        String s = Long.toString(a & 0xFF, 2);
        int len=s.length();
        while(len<8){
            s='0'+s;
            len++;
        }
        return s;
    }

    public static String bytes2String(byte[] a) {
        String re = "";
        for (Byte by : a) {
            re += byte2String(by);
        }
        return re;
    }

    public static String bytes2Int (byte[] a) {
//        String re = "";
//        for (Byte by : a) {
//            re += byte2Int(by);
//        }
//        return re;
        String hex = bytes2Hex(a);
        Integer x = Integer.parseInt(hex,16);
        return String.valueOf(x);
    }


    public static String bytes2IntS (byte[] a) {
        String re = "";
        for (Byte by : a) {
            re += byte2Int(by);
        }
        return re;
    }

    public static String bytes2Fen (byte[] a) {
        //byte[] length should be 3
        String f = byte2Int(a[0]);
        String f1 = byte2Int2(a[1]);
        String f2 = byte2Int2(a[2]);

        f = f+"."+f1+f2;
        float r = Float.valueOf(f)/60;
        DecimalFormat decimalFormat = new DecimalFormat(".000000");
        String tmp=decimalFormat.format(r);
        tmp.indexOf(".");
        return tmp.substring(tmp.indexOf(".")+1,tmp.length());
//        String hex = bytes2Hex(a);
//        Integer x = Integer.parseInt(hex,16);
//        return String.valueOf(x);
    }


    public static String bytes2Hex (byte[] a) {
        String hex = "";
        for (Byte by : a) {
            hex += byte2Hex(by);
        }
        return hex;
    }

    private  String bytes2DN (byte[] a) {
        String hex = bytes2Hex(a);
        int number = Integer.parseInt(hex,16);
        return "CNTL"+String.format("%010d",number);
    }

	public boolean validate () {
        return true;
	}

    @Override
    public String toString() {

        return "Header: " + header + "\n"
                +"Length: " + len + "\n"
                +"ID: " + id + "\n"
                +"DN: " + dn + "\n"
                + body.toString();
    }

    public ElectronicLockMessage toDomain() {
        ElectronicLockMessage elm = new ElectronicLockMessage();
        elm.setRawRow(raw_string);
        elm.setElNumber(dn);
        elm.setMid(id);
        elm.setLen(len);
        elm.setHeader(header);
        body.toDomain(elm);
        return elm;
    }

}
